function makeMatrixData(){
    matrixData= {};
    matrixDataArray=[];
    labels = unique(analyTypelist);
    for (let  i =0;i<labels.length;i++)
    {
        for (let j=0;j<labels.length;j++)
        {
            matrixData[labels[i]+"_"+labels[j]]=[i,j,0,labels[i],labels[j]];
        }
    }
    for (let i=0;i<analyTypelist.length;i++)
    {
        matrixData[analyTypelist[i]+"_"+preTypelist[i]][2]++;
    }
    for(let k in matrixData)
    {
        matrixDataArray.push(matrixData[k]);
    }
    return matrixDataArray;
}
function drawMatrix()
{
    drawData = makeMatrixData();
    const chart = new Chart();
    const config = {
        margins: {top: 50, left: 60, bottom: 0, right: 0},
        textColor: 'black',
        gridColor: 'gray',
    }
    chart.margins(config.margins);
    chart.width(250);
    chart.height(250);

    rectWidth =  chart.getBodyWidth()/Math.sqrt(drawData.length);
    chart.ScaleWidth = d3.scaleLinear().domain([0,d3.max(drawData,function (d,i){return d[2]})]).range([0,rectWidth/2]);
    chart.renderMatrix = function (){
        let rectEnters = chart.body().selectAll('rect').data(drawData).enter();
        rectEnters.append("rect")
            .attr("x",(d)=>d[0]*rectWidth)
            .attr("y",(d)=>d[1]*rectWidth)
            .attr("width",rectWidth)
            .attr("height",rectWidth)
            .style("fill","white")
            .style("stroke","black")
            .style("stroke-width",1);
        rectEnters.append("rect")
            .attr("x",(d)=>d[0]*rectWidth+2)
            .attr("y",(d)=>d[1]*rectWidth+2)
            .attr("width",(d)=>chart.ScaleWidth(d[2]))
            .attr("height",rectWidth/4)
            .style("fill",(d)=>chart._colors[d[3]+1]);
        rectEnters.append("rect")
            .attr("x",(d)=>d[0]*rectWidth+2)
            .attr("y",(d)=>d[1]*rectWidth+2+rectWidth/4)
            .attr("width",(d)=>chart.ScaleWidth(d[2]))
            .attr("height",rectWidth/4)
            .style("fill",(d)=>chart._colors[d[4]+1]);
        rectEnters.append("text")
            .attr("x",(d)=>d[0]*rectWidth+2+rectWidth/2)
            .attr("y",(d)=>d[1]*rectWidth+12+rectWidth/2)
            .text(function (d,i){
                if(d[2]!=0)
                    return d[2];
                else
                    return "";
            });
    }
    chart.renderLabel = function ()
    {
        labelRect = chart.svg().selectAll('.label').data(labels).enter();
        chart.svg().append("text")
            .attr("x",chart.width()/2)
            .attr("y",15)
            .text("聚类结果");
        labelRect.append("rect")
            .attr("x",(d,i)=>(i+1)*rectWidth)
            .attr("y",18)
            .attr("width",40)
            .attr("height",15)
            .style("fill",(d,i)=>chart._colors[i+1]);
        labelRect.append("text")
            .attr("x",(d,i)=>(i+1)*rectWidth+2)
            .attr("y",30)
            .text((d)=>"type"+d);
         labelRect.append("text")
            .attr("x",(d,i)=>(i+1)*rectWidth+2)
            .attr("y",45)
            .text(function (d,i){
                let a= 0;
                let b=0;
                for (let k of drawData)
                {
                    if(k[0]==i)
                    {
                        a += k[2]
                        if (k[1]==i)
                            b=k[2]
                    }
                }
                c = b/a;
                return c.toFixed(2);
            });

         chart.svg().append("text")
            .attr("x",15)
            .attr("y",160)
             .attr("transform","rotate(-90 15,160)")
            .text("解释结果");
          labelRect.append("rect")
            .attr("y",(d,i)=>(i+1)*rectWidth-10)
            .attr("x",18)
            .attr("width",40)
            .attr("height",15)
            .style("fill",(d,i)=>chart._colors[i+1]);
        labelRect.append("text")
            .attr("y",(d,i)=>(i+1)*rectWidth+2)
            .attr("x",21)
            .text((d)=>"type"+d);

        labelRect.append("text")
            .attr("y",(d,i)=>(i+1)*rectWidth+17)
            .attr("x",25)
            .text(function (d,i){
                let a= 0;
                let b=0;
                for (let k of drawData)
                {
                    if(k[1]==i)
                    {
                        a += k[2]
                        if (k[0]==i)
                            b=k[2]
                    }
                }
                c = b/a;
                return c.toFixed(2);
            });

    }
    chart.render = function(){
        chart.renderMatrix();
        chart.renderLabel();
    }
    chart.box(d3.select("#MatrixView"));
    chart.renderChart();
}